package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.SuccessData;
import com.jzg.jcptadmin.viewinterface.ChangePdInterface;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by jzg on 2016/1/12.
 */
public class ChangePdPresenter extends BasePresenter <ChangePdInterface>{
    private DataManager dataManager;
    private ChangePdInterface mView;
    private Subscription mSubscription;

    public ChangePdPresenter(DataManager dataManager){
        this.dataManager = dataManager;
    }
    @Override
    public void attachView(ChangePdInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;
    }
    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    public void changePd(String UserId ,String OldPassword,String NewPassword){

        dataManager.changePd(UserId,OldPassword,NewPassword).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Observer<SuccessData>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if(e!=null&&mView!=null){

                    mView.showError(e.getMessage());
                }
            }

            @Override
            public void onNext(SuccessData successData) {
                mView.showResult(successData);
            }
        });

    }

}
